<script>
export  default {
  name:"RecordManage",
  data() {
    let checkCount = (rule, value, callback) => {
      if(value>9999){
        callback(new Error('数量输⼊过⼤'));
      }else{
        callback();
      }
    };
    return {
      options: [{
        value: '0',
        label: '待付款'
      }, {
        value: '1',
        label: '待发货'
      }, {
        value: '2',
        label: '已发货'
      },{
        value: '3',
        label: '已收货'
      },{
        value: '4',
        label: '售后中'
      },{
        value: '5',
        label: '已完成'
      }],
      goodsData:[],
      tableData: [],
      total:0,
      goods: '',
      userName: '1',
      centerDialogVisible:false,
      form:{
        id:'',
        total: '',
        status:'',
      },
      rules: {
        name: [
          { required: true, message: '请输入物品名', trigger: 'blur' },
        ],
        count: [
          {required: true, message: '请输⼊数量', trigger: 'blur'},
          {pattern: /^([1-9][0-9]*){1,4}$/,message: '数量必须为正整数字',trigger: "blur"},
          {validator:checkCount,trigger: 'blur'}
        ],

      }
    }
  },
  methods:{
    formatGoods(row){
      let temp = this.goodsData.find(item=>{
        return item.id == row.goods
      })
      return temp && temp.name
    },
    getStatusText(status) {
      switch (status) {
        case '0':
          return '待付款';
        case '1':
          return '待发货';
        case '2':
          return '已发货';
        case '3':
          return '已收货';
        case '4':
          return '售后中';
        case '6':
          return '退单申请';
        default:
          return '已完成';
      }
    },
    loadPost(){
      this.$axios.get('http://localhost:9098/order/all').then(res=>res.data).then(res=> {
        console.log(res)
        if(res.statusCode==='200'){
          this.tableData=res.data
          this.total=this.tableData.length;
        }else{
          alert('获取数据失败')
        }
        //this.tableData=res
      })
    },
    /*loadGoods(){
      this.$axios.get(this.$httpUrl+'/product/list').then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.goodstypeData=res.data
        }else{
          alert('获取数据失败')
        }
        //this.tableData=res
      })
    },*/
    resetParam(){
      this.name=''
      this.goods=''
    },
    add(){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm();
        this.form.id = ''
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.form.id){
            this.doMod();
          }else {
            this.doSave();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    doSave(){
      this.$axios.post(this.$httpUrl+'/record/save',this.form).then(res=>res.data).then(res=> {
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else{
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
        //this.tableData=res
      })
    },
    doMod(){
      this.$axios.post('http://localhost:9098/order/updateOrder',{orderId: this.form.id,total:this.form.total,
        status:this.form.status
      }).then(res=>res.data).then(res=> {
        console.log(res)
        if(res.statusCode==='200'){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
        }else{
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
        //this.tableData=res
      })
    },
    resetForm(){
      this.$refs.form.resetFields();
    },
    del(id){
      console.log(id)
      this.$axios.get('http://localhost:9098/order/delete/'+id).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.statusCode === '200'){
          this.$message({
            message:'操作成功！',
            type:'success'
          });
          //this.centerDialogVisible=false
          this.loadPost()
          //this.resetForm()
        }
        else{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }
      })
    },
    mod(row){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        //赋值到表单
        this.form.id = row.orderIdString,
        this.form.total = row.total
        this.form.status = row.status
      })
    }
  },
  beforeMount() {
    //this.loadGoods()


    this.loadPost();

  }
}
</script>

<template>
  <div>
<!--    <div style="margin-bottom: 5px">-->
<!--      <el-input v-model="name" placeholder="请输入物品名" suffix-icon="el-icon-search" style="width: 200px" @keyup.enter.native="loadPost"></el-input>-->
<!--      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>-->
<!--      <el-button type="success" style="margin-left: 5px" @click="resetParam">重置</el-button>-->
<!--    </div>-->
    <el-table :data="tableData"
              :header-cell-style="{ background: '#f2f5fc', color: '#555555'}"
              border
    >
      <el-table-column prop="orderIdString" label="订单编号" align="center" width="200px">
      </el-table-column>
      <el-table-column prop="userName" label="用户" align="center">
      </el-table-column>
      <el-table-column prop="total" label="总价" align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="120px">
        <template slot-scope="scope">
          <span>{{ getStatusText(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center">
      </el-table-column>
      <el-table-column prop="info" label="原因" align="center">
      </el-table-column>
      <el-table-column prop="operate" label="操作" align="center">
        <template v-slot="scope">
          <el-button type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定退单吗？"
              @confirm="del(scope.row.orderIdString)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference"  type="danger" >退单</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="价格" prop="total">
          <el-col :span="20">
            <el-input v-model="form.total"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-col :span="20">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>